'''
Created on 28/09/2011

@author: Ike-kun <ragnell22@gmail.com>
'''

from LogManager import LogManager
from Tkinter import LabelFrame,Toplevel
import Tix as tix

class ConfigViewer(Toplevel):
    def __init__(self,parent):
        Toplevel.__init__(self,parent)
        self.transient(parent)
        self.parent = parent
        self.logManager = LogManager()
        self.geometry("%dx%d+%d+%d" % (285,200,300,250))
        self.protocol("WM_DELETE_WINDOW",self.cancel)
        self.title("Edit Configuration File")
        self.configure(bg="gray")
        self.resizable(0,0)
        self.grab_set()
        self.createComboBoxes()
        self.createButtons()
        self.wait_window(self)

    def createComboBoxes(self):
        labelframe = LabelFrame(self,text="Parameters",bg="gray")
        labels = ["Format:","Target:","Time Format:","Log Level"]
        listOfLists = [["Short","Medium","Long","Debug"],
                       ["console","csv","xml"],
                       ["Full","LongTime","ShortTime"],
                       ["NONE","NORMAL","VERBOSE","DEBUG"]]
        methodList = [self.selectedFormat,self.selectedTarget,self.selectedTimeFormat,self.selectedLogLevel]
        iniValues = self.logManager.getEverything()
        for i in range(len(labels)):
            tix.Label(labelframe,text=labels[i],relief=tix.RIDGE,width=18,bg="gray").grid(row=i,column=0)
            combo = tix.ComboBox(labelframe)
            itemList = listOfLists[i]
            for item in itemList:
                combo.slistbox.listbox.insert('end',item)
            combo.slistbox.listbox.bind('<ButtonRelease-1>',methodList[i])
            combo.slistbox.listbox.configure(height=len(itemList),bg='white')
            combo.grid(row=i,column=1,sticky=tix.W)
            combo.configure(value=iniValues[i])
        labelframe.pack()

    def createButtons(self):
        area = tix.Frame(self,bg="gray")
        a = ["Exit","Save"]
        color = 'gray'
        tix.Button(area,text=a[0],width=12,bg=color,command=self.cancel).grid(row=5,column=0)
        tix.Button(area,text=a[1],width=12,bg=color,command=self.save).grid(row=5,column=1)
        area.pack(pady=30)

    def selectedFormat(self,event):
        selectedValue = event.widget.curselection()[0]
        comboSelection = event.widget.get(selectedValue)
        self.logManager.setFormat(comboSelection)

    def selectedTarget(self,event):
        selectedValue = event.widget.curselection()[0]
        comboSelection = event.widget.get(selectedValue)
        self.logManager.setTarget(comboSelection)

    def selectedTimeFormat(self,event):
        selectedValue = event.widget.curselection()[0]
        comboSelection = event.widget.get(selectedValue)
        self.logManager.setTimeFormat(comboSelection)

    def selectedLogLevel(self,event):
        selectedValue = event.widget.curselection()[0]
        comboSelection = event.widget.get(selectedValue)
        self.logManager.setLogLevel(comboSelection)

    def save(self):
        self.logManager.saveConf()
        self.cancel()

    def cancel(self,event=None):
        self.parent.focus_set()
        self.destroy()
